package blog.controller;

import blog.entity.Option;
import blog.service.OptionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@Controller @RequestMapping("options")
public class OptionsController {
    @Resource
    private OptionService optionService;
    @RequestMapping("")
    public String gotoOptions(Map map){
        Option option = optionService.getOption();
        map.put("option",option);
        return "options/options";
    }
    @RequestMapping("/editOptions")
    public String editOption(Option option, MultipartFile avatar,MultipartFile weChat) throws IOException {
        option.setOptionAboutsiteAvatar(avatar.getBytes());
        option.setOptionAboutsiteWechat(weChat.getBytes());
        optionService.updateOption(option);
        return "forward:/options";
    }
    @RequestMapping("/avator")
    public void avator(HttpServletResponse response) throws IOException {
        Option option = optionService.getOption();
        response.setContentType("image/jpg");
        ServletOutputStream outStream=response.getOutputStream();
        outStream.write(option.getOptionAboutsiteAvatar());
        outStream.flush();
    }
    @RequestMapping("/weChat")
    public void wechat(HttpServletResponse response) throws IOException {
        Option option = optionService.getOption();
        response.setContentType("image/jpg");
        ServletOutputStream outStream=response.getOutputStream();
        outStream.write(option.getOptionAboutsiteWechat());
        outStream.flush();
    }
}
